package org.jeecg.modules.message.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jeecg.modules.message.entity.ProcurementMessage;

import java.util.Date;
import java.util.List;

/**
 * @Description: 采购消息
 * @Author: jeecg-boot
 * @Date: 2023-09-21
 * @Version: V1.0
 */
@Mapper
public interface ProcurementMessageMapper extends BaseMapper<ProcurementMessage> {

    /**
     * 根据接收人获取未读消息
     * @param receiverId 接收人
     * @return 未读消息列表
     */
    @Select("SELECT * FROM proc_message WHERE receiver_id = #{receiverId} AND read_status = '0' AND is_deleted = '0' ORDER BY create_time DESC")
    List<ProcurementMessage> getUnreadMessagesByReceiver(@Param("receiver") String receiverId);

    /**
     * 根据业务类型和业务ID获取消息
     * @param businessType 业务类型
     * @param businessId 业务ID
     * @return 消息列表
     */
    @Select("SELECT * FROM proc_message WHERE business_type = #{businessType} AND business_id = #{businessId} AND is_deleted = '0' ORDER BY create_time DESC")
    List<ProcurementMessage> getMessagesByBusiness(@Param("businessType") String businessType, @Param("businessId") String businessId);

    /**
     * 批量更新消息阅读状态
     * @param messageIds 消息ID列表
     * @param readStatus 阅读状态
     * @param readTime 阅读时间
     * @return 更新数量
     */
    @Update("<script>UPDATE proc_message SET read_status = #{readStatus} <if test='readTime != null'>, read_time = #{readTime}</if> WHERE id IN <foreach collection='messageIds' item='id' open='(' separator=',' close=')'>#{id}</foreach></script>")
    int batchUpdateReadStatus(@Param("messageIds") List<String> messageIds, @Param("readStatus") String readStatus, @Param("readTime") Date readTime);

    /**
     * 根据接收人获取消息数量统计
     * @param receiverId 接收人ID
     * @return 消息数量统计
     */
    @Select("SELECT COUNT(*) FROM proc_message WHERE receiver_id = #{receiverId} AND is_deleted = '0'")
    int getMessageCountByReceiver(@Param("receiver") String receiverId);

    /**
     * 根据接收人获取未读消息数量
     * @param receiverId 接收人ID
     * @return 未读消息数量
     */
    @Select("SELECT COUNT(*) FROM proc_message WHERE receiver_id = #{receiverId} AND read_status = '0' AND is_deleted = '0'")
    int getUnreadMessageCountByReceiver(@Param("receiverId") String receiverId);

    /**
     * 软删除消息
     * @param messageIds 消息ID列表
     * @return 删除数量
     */
    @Update("<script>UPDATE proc_message SET is_deleted = '1' WHERE id IN <foreach collection='messageIds' item='id' open='(' separator=',' close=')'>#{id}</foreach></script>")
    int softDeleteMessages(@Param("messageIds") List<String> messageIds);

    /**
     * 根据消息类型获取消息
     * @param messageType 消息类型
     * @param receiver 接收人ID
     * @return 消息列表
     */
    @Select("SELECT * FROM proc_message WHERE message_type = #{messageType} AND receiver_id = #{receiverId} AND is_deleted = '0' ORDER BY create_time DESC")
    List<ProcurementMessage> getMessagesByType(@Param("messageType") String messageType, @Param("receiverId") String receiverId);
}
